<?php

declare(strict_types=1);

namespace App\Controller\api;

use App\Common\CommonAbstract;
use App\Constants\Express;
use App\Kernel\Express\KdBird;
use App\Service\Store\ExpressService;
use App\Service\Store\OrderService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;

/**
 * 快递回调控制器
 *
 * @AutoController()
 * @author 陈奕文
 * @package App\Controller\api
 */
class ExpressNotifyController extends CommonAbstract
{

    /**
     * 订单服务
     * @Inject()
     * @var OrderService
     */
    private $order_service;

    /**
     * 快递服务
     * @Inject()
     * @var ExpressService
     */
    private $express_service;

    /**
     * @Inject()
     * @var KdBird
     */
    private $kd_bird;

    /**
     * 快递鸟订阅
     * @return array
     */
    public function bird()
    {
        $params = $this->request->all();

//        if (!isset($params['RequestData']) || !isset($params['DataSign']))
//            return $this->kd_bird->show(false, "没有两个参数");
//
//        if ( $this->kd_bird->encryptNotUrlEncode($params['RequestData'], config('express.app_key')) !== $params['DataSign'] ) // 加密判断是否是快递鸟来的
//            return $this->kd_bird->show(false, "非快递鸟请求的接口");

//        $requestData = $params['RequestData']; // 测试的数组
        $requestData = json_decode($params['RequestData'], true); // 完全从stdclass转为数组
        $result = null;
        $datas = $requestData['Data'];
        switch ($params['RequestType']) {
            case Express::BIRD_QUERY_RESULT: // 查询订阅结果 轨迹推送接口

                $result = $this->express_service->trackPush($datas);

                break;
            case Express::BIRD_QUERY_RESULT_ADD_VERSION: // 查询订阅结果 轨迹推送接口（增值版）

                $result = $this->express_service->trackPushAdd($datas);
                break;

        }

        return $result;

    }


    /**
     *  快递物流测试
     */
    public function logistics()
    {
        $params = $this->request->all();

//        $logistic = $this->order_service->logistics_dao->find([
//            ['number' => $params['number'],],
//        ]);

        $data = [ // 物流参数示例
          'ShipperCode' => 'STO',
          'LogisticCode' => '773037130596875',
            'PayType' => 2,
            'Receiver' => [
                'Name' => '傅艳婷',
                'Mobile' => '18273668422',
                'ProvinceName' => '广东省',
                'CityName' => '深圳市',
                'ExpAreaName' => '龙华区',
                'Address' => '清湖地区',
            ],
            'Sender' => [
                'Name' => '发件者',
                'Mobile' => '12432534643',
                'ProvinceName' => '广东省',
                'CityName' => '深圳市',
                'ExpAreaName' => '龙华区',
                'Address' => '清湖地区',
            ],
        ];

        $resp = $this->kd_bird->orderTracesSubByJson($data);
        $this->success($resp);
    }

}
